package com.ruoyi.base.service.impl;

import java.util.List;

import com.ruoyi.base.ch340.Ch340Configure;
import com.ruoyi.base.ch340.Ch340Service;
import com.ruoyi.base.tool.CHeck16;
import com.ruoyi.base.tool.TypeConverter;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.WaterSetMapper;
import com.ruoyi.base.domain.WaterSet;
import com.ruoyi.base.service.IWaterSetService;

/**
 * 设置Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-28
 */
@Service
public class WaterSetServiceImpl implements IWaterSetService
{
    @Autowired
    private WaterSetMapper waterSetMapper;

    @Autowired
    Ch340Service ch340Service;

    /**
     * 查询设置
     *
     * @param id 设置主键
     * @return 设置
     */
    @Override
    public WaterSet selectWaterSetById(Long id)
    {
        return waterSetMapper.selectWaterSetById(id);
    }

    /**
     * 查询设置列表
     *
     * @param waterSet 设置
     * @return 设置
     */
    @Override
    public List<WaterSet> selectWaterSetList(WaterSet waterSet)
    {
        return waterSetMapper.selectWaterSetList(waterSet);
    }

    /**
     * 新增设置
     *
     * @param waterSet 设置
     * @return 结果
     */
    @Override
    public int insertWaterSet(WaterSet waterSet)
    {

        String hex1 = waterSet.getHex1();
        String hex2 = waterSet.getHex2();
        String hex3 = waterSet.getHex3();
        String hex4 = waterSet.getHex4();
        String hex5 = waterSet.getHex5();
        String hex6 = waterSet.getHex6();
        String hex7 = waterSet.getHex7();
        String hex8 = waterSet.getHex8();
        String hex9 = waterSet.getHex9();
        String hex10 = waterSet.getHex10();
        String hex11 = waterSet.getHex11();
        String hex12 = waterSet.getHex12();

        String hex = TypeConverter.noNull(hex1) + "-" +
                TypeConverter.noNull(hex2) + "-" +
                TypeConverter.noNull(hex3) + "-" +
                TypeConverter.noNull(hex4) + "-" +
                TypeConverter.noNull(hex5) + "-" +
                TypeConverter.noNull(hex6) + "-" +
                TypeConverter.noNull(hex7) + "-" +
                TypeConverter.noNull(hex8) + "-" +
                TypeConverter.noNull(hex9) + "-" +
                TypeConverter.noNull(hex10) + "-" +
                TypeConverter.noNull(hex11) + "-" +
                TypeConverter.noNull(hex12) ;

        waterSet.setHex(hex);


        try {
            String send  = Ch340Configure.X18 + TypeConverter.type10to16v2(hex1) +
                    TypeConverter.type10to16v2(hex2) +
                    TypeConverter.type10to16v2(hex3) +
                    TypeConverter.type10to16v2(hex4) +
                    TypeConverter.type10to16v2(hex5) +
                    TypeConverter.type10to16v2(hex6) +
                    TypeConverter.type10to16v2(hex7) +
                    TypeConverter.type10to16v2(hex8) +
                    TypeConverter.type10to16v2(hex9) +
                    TypeConverter.type10to16v2(hex10) +
                    TypeConverter.type10to16v2(hex11) +
                    TypeConverter.type10to16v2(hex12) ;

            String write = CHeck16.write(send);

            String read = ch340Service.sendAndRead(write);
            CHeck16.read(read);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }


        waterSet.setCreateTime(DateUtils.getNowDate());
        return waterSetMapper.insertWaterSet(waterSet);
    }

    /**
     * 修改设置
     *
     * @param waterSet 设置
     * @return 结果
     */
    @Override
    public int updateWaterSet(WaterSet waterSet)
    {
        return waterSetMapper.updateWaterSet(waterSet);
    }

    /**
     * 批量删除设置
     *
     * @param ids 需要删除的设置主键
     * @return 结果
     */
    @Override
    public int deleteWaterSetByIds(Long[] ids)
    {
        return waterSetMapper.deleteWaterSetByIds(ids);
    }

    /**
     * 删除设置信息
     *
     * @param id 设置主键
     * @return 结果
     */
    @Override
    public int deleteWaterSetById(Long id)
    {
        return waterSetMapper.deleteWaterSetById(id);
    }
}
